home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / s342q12.lha / netcall.c < prev    next >
C/C++ Source or Header  |  1996-03-16  |  30KB  |  1,200 lines

  1. /*
  2. *       netcall.c
  3. *
  4. * Networking call functions.
  5. */
  6. /*
  7. *       history
  8. *
  9. * 86Aug20 HAW  History not maintained due to space problems.
  10. */
  11. #include "ctdl.h"
  12. #include "math.h"
  13. /*
  14. *       contents
  15. *
  16. */
  17. char                    inReceive;
  18. NetInfo     NetStyle;
  19. extern char   RecMassTransfer;
  20. extern char             *SR_Sent;
  21. extern char             *pollCall;
  22. extern FILE             *netMisc;
  23. extern FILE             *netLog;
  24. extern AN_UNSIGNED      RecBuf[SECTSIZE + 5];
  25. extern int              counter;
  26. extern int              callSlot;
  27. extern label            callerName, callerId;
  28. char                    checkNegMail;
  29. extern char             processMail;
  30. extern char   MassTransferSent;
  31. char             normId(), getNetMessage();
  32. AN_UNSIGNED      inp();
  33. extern CONFIG    cfg;            /* Lots an lots of variables    */
  34. extern logBuffer logBuf;         /* Person buffer                */
  35. extern logBuffer logTmp;         /* Person buffer                */
  36. extern aRoom     roomBuf;        /* Room buffer                  */
  37. extern rTable    *roomTab;
  38. extern PROTO_TABLE Table[];
  39. extern int TransProtocol;
  40. extern MessageBuffer   msgBuf;
  41. extern NetBuffer netBuf, netTemp;
  42. extern NetTable  *netTab;
  43. /* extern SListBase FwdAliasii; */
  44. extern int       thisNet;
  45. extern char      onConsole;
  46. extern char      loggedIn;       /* Is we logged in?             */
  47. extern char      outFlag;        /* Output flag                  */
  48. extern char      haveCarrier;    /* Do we still got carrier?     */
  49. extern char      modStat;        /* Needed so we don't die       */
  50. extern char      TrError;
  51. extern int       thisRoom;
  52. extern char      netDebug;
  53. extern char      logNetResults;
  54. extern char  *DomainFlags;
  55. extern long ByteCount, EncCount;
  56. char *chMailTemplate = "chkMail.$$$";
  57. extern long  char_in, char_out; /* send/recieve character counts*/
  58. long start_time;                /* total time of session */
  59. /*
  60. * caller()
  61. *
  62. * This function is called when we've been called and have to handle the
  63. * caller.  We have to stabilize the call and then manage all requests
  64. * the caller makes of us.
  65. */
  66. void caller()
  67.   {
  68.   ITL_InitCall();             /* initialize the ITL layer */
  69.   RecMassTransfer = FALSE;
  70.   memset(SR_Sent, 0, SHARED_ROOMS);
  71.   inReceive = FALSE;
  72.   processMail = FALSE;
  73.   checkNegMail = FALSE;
  74.   SpecialMessage("Status:Net Carrier");
  75.   if( logNetResults && netDebug )splitF(netLog, "Have Carrier\n");
  76.   caller_stabilize();
  77.   if (!haveCarrier) return ;  /* Abort */
  78.   if( logNetResults )splitF(netLog, "Stabilized\n");
  79.   SpecialMessage("Status:Net Session");
  80.   char_in = char_out = 0;
  81.   start_time = Set_Timer(0);
  82.   sendId();
  83.   if (!haveCarrier) return ;  /* Abort */
  84.   if (!netBuf.nbflags.MassTransfer)
  85.   ITL_optimize(TRUE);   /* try for better protocol */
  86.   sendStuff(FALSE, TRUE);
  87.   startTimer(WORK_TIMER);
  88.   while (gotCarrier() && chkTimeSince(WORK_TIMER) < 10) ;
  89.   killConnection();
  90.   if( logNetResults )splitF(netLog, "\nFinished with %s @%s\n", netBuf.netName, Current_Time());
  91.   Compute_Data(netBuf.netName);
  92.   doResults();
  93.   SpecialMessage("Status:Net Completed");
  94.  
  95.   }
  96. /*
  97. * sendStuff()
  98. *
  99. * This function handles being the sender of information (sending role).
  100. */
  101. void sendStuff(char reversed, char SureDoIt)
  102.   {
  103.   extern int RouteToDirect;
  104.   if (SureDoIt && callSlot != ERROR)
  105.     {
  106.     MassTransferSent = FALSE;
  107.     SendPwd();
  108.     if (!haveCarrier) return ;  /* Abort */
  109.     if (netBuf.nbflags.HasRouted)    RouteOut();
  110.     if (!haveCarrier) return ;  /* Abort */
  111.     if (!netBuf.nbflags.Stadel) DomainOut(FALSE);
  112.     if (!haveCarrier) return ;  /* Abort */
  113.     if (netBuf.nbflags.normal_mail ||
  114.     RouteToDirect != -1 ||
  115.     (netBuf.nbflags.Stadel && DomainFlags[thisNet]))
  116.       {
  117.       sendMail();
  118.       if (!haveCarrier) return ;  /* Abort */
  119.       checkMail();
  120.  
  121.       }
  122.     if (!haveCarrier) return ;  /* Abort */
  123.     if (!HasPriorityMail(thisNet))
  124.       {
  125.       if (netBuf.nbflags.room_files)
  126.       askFiles();
  127.       if (!haveCarrier) return ;  /* Abort */
  128.       sendSharedRooms();
  129.       if (!haveCarrier) return ;  /* Abort */
  130.       if (netBuf.nbflags.send_files)
  131.       doSendFiles();
  132.       if (!haveCarrier) return ;  /* Abort */
  133.       roleReversal(reversed);
  134.  
  135.       }
  136.  
  137.     }
  138.   sendHangUp();
  139.   pollCall[thisNet]--;        /* Don't set polled flag unless stable call */
  140.  
  141.   }
  142. /*
  143. * SendPwd()
  144. *
  145. * This function sends the system password if necessary.
  146. */
  147. void SendPwd()
  148.   {
  149.   struct cmd_data cmds;
  150.   if (netBuf.TheirPwd[0] != 0)
  151.     {
  152.     /* only send if need to -- gets */
  153.     zero_struct(cmds);              /* us around a bug in pre net 1.10*/
  154.     strCpy(cmds.fields[0], netBuf.TheirPwd);        /* versions     */
  155.     cmds.command = SYS_NET_PWD;
  156.     sendNetCommand(&cmds, "system pwd");
  157.  
  158.     }
  159.  
  160.   }
  161. /*
  162. * roleReversal()
  163. *
  164. * This function handles the role reversal request.
  165. */
  166. void roleReversal(char reversed)
  167.   {
  168.   struct cmd_data cmds;
  169.   if (reversed) return ;
  170.   if (!netBuf.nbflags.local && !netBuf.nbflags.spine) return ;
  171.   if( logNetResults && netDebug )splitF(netLog, "Reversing roles\n");
  172.   zero_struct(cmds);
  173.   cmds.command = ROLE_REVERSAL;
  174.   if (!sendNetCommand(&cmds, "role reversal"))
  175.   return;
  176.   rcvStuff(TRUE);
  177.   pause(50);   /* wait a second */
  178.   if (gotCarrier())
  179.   reply(GOOD, "");/* this replies GOOD to the HANGUP terminating the */
  180.   /* role reversal.  NOTE: STadel doesn't follow the */
  181.   /* spec in this regard - it just dumps carrier     */
  182.  
  183.   }
  184. /*
  185. * caller_stabilize()
  186. *
  187. * This function tries to stabilize the call -- baud is already set.
  188. */
  189. void caller_stabilize()
  190.   {
  191.   int tries, x1, x2, x3;
  192.   extern char hst;
  193.   /* regrettable initialization */
  194.   for (tries = 0; tries < SHARED_ROOMS; tries++) resetNeedsProcessing(tries);
  195.   putNet(thisNet, &netBuf);
  196.   pause(50);                                  /* delay for a little bit */
  197.   while (MIReady())   inp();                  /* Clear garbage        */
  198.   startTimer(USER_TIMER);                     /* this is safe */
  199.   x1 = x2 = x3 = 0;
  200.   for (tries = 0; (chkTimeSince(USER_TIMER) < 20l || tries < 40) &&
  201.   gotCarrier() ; tries++)
  202.     {
  203.     outMod(7);
  204.     outMod(13);
  205.     outMod(69);
  206.     for (  startTimer(WORK_TIMER);
  207.     chkTimeSince(WORK_TIMER) < 2l && !MIReady();) ;
  208.     if (MIReady())
  209.       {
  210.       x1 = receive(2);
  211.       x2 = receive(2);
  212.       if (x2 != ERROR) x3 = receive(2);
  213.       if (x1 == 248 && x2 == 242 && x3 == 186)
  214.         {
  215.         outMod(ACK);
  216.         /* ok, we've seen at high speed some overrun problems, so ... */
  217.         do
  218.           {
  219.           x1 = receive(2);
  220.  
  221.           }
  222.         while (x1 == 248 || x1 == 242 || x1 == 186);
  223.         ModemPushBack(x1);
  224.         return;
  225.  
  226.         }
  227.       else
  228.         {
  229.         if ( ( x1 == 242 || x1 == 186 || x1 == 248 )
  230.         || ( x2 == 242 || x2 == 186 || x2 == 248 )
  231.         || ( x3 == 242 || x3 == 186 || x3 == 248 ) )  /* all combinations */
  232.           {
  233.           /* real close, so let's catch our breath */
  234.           while (receive(1) != ERROR) ;
  235.  
  236.           };
  237.  
  238.         }
  239.  
  240.       }
  241.     else
  242.       {
  243.       tries ++;  /* if we get nothing back, don't try as much, try half the number of times */
  244.  
  245.       }
  246.  
  247.     }
  248.   if( logNetResults && netDebug )splitF(netLog, "Call not stabilized:tries=%d \n",tries);
  249.   killConnection();
  250.  
  251.   }
  252. /*
  253. * sendId()
  254. *
  255. * This function sends ID to the receiver.
  256. */
  257. void sendId()
  258.   {
  259.   if (!ITL_Send(STARTUP))
  260.     {
  261.     no_good("Couldn't transfer ID to %s at startup!", TRUE);
  262.     return;
  263.  
  264.     }
  265.   mTrPrintf("%s", cfg.codeBuf + cfg.nodeId  );
  266.   mTrPrintf("%s", cfg.codeBuf + cfg.nodeName);
  267.   if (!ITL_Send(FINISH))
  268.     {
  269.     no_good("Couldn't transfer ID to %s at finish!", TRUE);
  270.     return;
  271.  
  272.     }
  273.  
  274.   }
  275. /*
  276. * sendMail()
  277. *
  278. * This function sends normal mail to receiver.
  279. */
  280. void sendMail()
  281.   {
  282.   struct cmd_data cmds;
  283.   int             nor_mail;
  284.   extern int      RouteToDirect;
  285.   if (!gotCarrier())
  286.     {
  287.     modStat = haveCarrier = FALSE;
  288.     return ;
  289.  
  290.     }
  291.   if( logNetResults )splitF(netLog, "Sending Mail ");
  292.   zero_struct(cmds);
  293.   cmds.command = NORMAL_MAIL;
  294.   if (!sendNetCommand(&cmds, "normal mail"))
  295.   return;
  296.   if (!ITL_SendMessages())
  297.     {
  298.     no_good("Couldn't start Mail transfer to %s!", TRUE);
  299.     killConnection();
  300.     return;
  301.  
  302.     }
  303.   nor_mail = s_m_n();            /* Send normal mail     */
  304.   if (gotCarrier())
  305.   nor_mail += SendRoutedAsLocal();
  306.   if (gotCarrier() && netBuf.nbflags.Stadel)
  307.   nor_mail += DomainOut(TRUE);
  308.   ITL_StopSendMessages();
  309.   RouteToDirect = -1;         /* This is just in case */
  310.   if (gotCarrier())
  311.     {
  312.     if( logNetResults )splitF(netLog, "(%d) (%ld => %ld bytes)\n",nor_mail,EncCount,ByteCount);
  313.     netBuf.nbflags.normal_mail = FALSE;
  314.  
  315.     }
  316.  
  317.   }
  318. /*
  319. * checkMail()
  320. *
  321. * This function handles negative acknowledgement on netMail.
  322. */
  323. void checkMail()
  324.   {
  325.   struct cmd_data cmds;
  326.   SYS_FILE fileNm;
  327.   extern char *WRITE_ANY;
  328.   if (!gotCarrier())
  329.     {
  330.     return;
  331.  
  332.     }
  333.   if( logNetResults && netDebug )splitF(netLog, "Check mail\n");
  334.   makeSysName(fileNm, chMailTemplate, &cfg.netArea);
  335.   zero_struct(cmds);
  336.   cmds.command = CHECK_MAIL;
  337.   if (!sendNetCommand(&cmds, "check mail"))
  338.     {
  339.     return;
  340.  
  341.     }
  342.   if (ITL_Receive(fileNm, FALSE, TRUE, putFLChar, fclose) == ITL_SUCCESS)
  343.   checkNegMail = TRUE;        /* Call readNegMail() later */
  344.  
  345.   }
  346. /*
  347. * readNegMail()
  348. *
  349. * This function reads and processes negative acks.
  350. */
  351. void readNegMail(char talk)
  352.   {
  353.   label author, target, context;
  354.   int whatLog;
  355.   int  sigChar;
  356.   SYS_FILE fileNm;
  357.   extern char *READ_ANY;
  358.   makeSysName(fileNm, chMailTemplate, &cfg.netArea);
  359.   if ((netMisc = safeopen(fileNm, READ_ANY)) == NULL)
  360.     {
  361.     if (talk) no_good("Couldn't open negative ack file from %s.", FALSE);
  362.     return ;
  363.  
  364.     }
  365.   getRoom(MAILROOM);
  366.   sigChar = fgetc(netMisc);
  367.   while (sigChar != NO_ERROR && sigChar != EOF && sigChar != EOF)
  368.     {
  369.     ZeroMsgBuffer(&msgBuf);
  370.     strCpy(msgBuf.mbauth, "Citadel");
  371.     getMsgStr(getNetChar, author, NAMESIZE);
  372.     getMsgStr(getNetChar, target, NAMESIZE);
  373.     getMsgStr(getNetChar, context, NAMESIZE);
  374.     switch (sigChar)
  375.       {
  376.       case NO_RECIPIENT:
  377.       strCpy(msgBuf.mbto, author);
  378.       if ((whatLog = PersonExists(author)) >= 0 &&
  379.       whatLog < cfg.MAXLOGTAB)
  380.         {
  381.         sPrintf(msgBuf.mbtext,
  382.         "Your netMail to '%s' (%s) failed because there is no such recipient on %s.",
  383.         target, context, callerName);
  384.         putMessage(&logBuf);
  385.         break;
  386.  
  387.         }
  388.       case UNKNOWN:
  389.       ZeroMsgBuffer(&msgBuf);
  390.       sPrintf(msgBuf.mbtext,
  391.       "Unknown problems with netMail: author=-%s-, target=-%s-, context=-%s-.  System was %s.",
  392.       author, target, context, netBuf.netName);
  393.       netResult(msgBuf.mbtext);
  394.       break;
  395.       case BAD_FORM:
  396.       sPrintf(msgBuf.mbtext, "Bad netMail sent to %s.", callerName);
  397.       netResult(msgBuf.mbtext);
  398.       break;
  399.       default:
  400.       sPrintf(msgBuf.mbtext, "Bad sigChar=%d.", sigChar);
  401.       netResult(msgBuf.mbtext);
  402.       break;
  403.  
  404.       }
  405.     sigChar = fgetc(netMisc);
  406.  
  407.     }
  408.   fclose(netMisc);
  409.   if( netDebug && logNetResults )
  410.     {
  411.      splitF(netLog,
  412.     "problems with netMail: author=-%s-, target=-%s-, context=-%s-.  System was %s.",
  413.       author, target, context, netBuf.netName);
  414.     };
  415.   unlink(fileNm);
  416.  
  417.   }
  418. /*
  419. * sendSharedRooms()
  420. *
  421. * This sends all shared rooms to receiver.
  422. */
  423. static int SendRoom(SharedRoom *room, int system, int index, int roomslot,void *d);
  424. void sendSharedRooms()
  425.   {
  426.   SendFastTransfer();
  427.   EachSharedRoom(thisNet, SendRoom, SendVirtualRoom, NULL);
  428.  
  429.   }
  430. /*
  431. * EachSharedRoom()
  432. *
  433. * This does something for each shared room.
  434. */
  435. void EachSharedRoom(int system,
  436. int (*func)(SharedRoom *room, int system, int index, int roomslot, void *d),
  437. int (*virtfunc)(VirtualRoom *room, int sys, int index, int which, void *d),
  438. void *data)
  439.   {
  440.   int rover;
  441.   if (!netTab[system].ntflags.in_use) return;
  442.   if (func != NULL)
  443.     {
  444.     for (rover = 0; rover < SHARED_ROOMS; rover++)
  445.       {
  446.       if (isSharedRoom(system, rover) && roomValidate(system, rover))
  447.         {
  448.         if ((*func)(netTab[system].netTRooms + rover, system,
  449.         rover, netTabRoomSlot(system, rover), data) == ERROR)
  450.         return;
  451.  
  452.         }
  453.  
  454.       }
  455.  
  456.     }
  457.   if (virtfunc != NULL) DoVirtuals(system, virtfunc, data);
  458.  
  459.   }
  460. /*
  461. * Addressing()
  462. *
  463. * This function is responsible for deciding what sort of addressing or routing
  464. * flags should be checked for, and if the room should be sent if we are in
  465. * a network session.
  466. */
  467. void Addressing(int system, int index, char *commnd, char **send1, char **send2,
  468. char **send3, char **name, char *doit)
  469.   {
  470.   extern char *R_SH_MARK, *NON_LOC_NET, *LOC_NET;
  471.   /*
  472.   * This is more than just a trivial efficiency.  This routine can be called
  473.   * indirectly by the room editing functions.  If this happens then the
  474.   * getRoom() call would overwrite the roomBuf being used for editing.
  475.   * Therefore, we can only read in roomBuf if we don't have the right one
  476.   * in place, otherwise we lose what we just changed.
  477.   */
  478.   if (thisRoom != netTabRoomSlot(system, index))
  479.   getRoom(netTabRoomSlot(system, index));
  480.   *doit = TRUE;
  481.   *send1 = R_SH_MARK;
  482.   *send2 = *send3 = "guh";
  483.   switch (roomBuf.rbShareType)
  484.     {
  485.     case PEON:
  486.     *commnd = NET_ROOM;
  487.     *name = "Peon";
  488.     break;
  489.     case REG_HOST:          /* obsolete */
  490.     case BACKBONE:
  491.     switch (GetMode(netTab[system].netTRooms[index].mode))
  492.       {
  493.       case PEON:
  494.       *commnd = NET_ROOM;
  495.       *send2 = NON_LOC_NET;
  496.       *name = "Peon";
  497.       break;
  498.       case ACTIVE_BACKBONE:
  499.       case REG_HOST:
  500.       if (netTab[system].ntflags.local)
  501.       *commnd = NET_ROOM;
  502.       else
  503.       *commnd = NET_ROUTE_ROOM;
  504.       *send2 = NON_LOC_NET;
  505.       *send3 = LOC_NET;
  506.       *name = "Backbone - Active";
  507.       break;
  508.       case PASS_BACKBONE:
  509.       if (!netTab[system].ntflags.local && !inReceive) *doit = FALSE;
  510.       else if (netTab[system].ntflags.local)
  511.       *commnd = NET_ROOM;
  512.       else
  513.       *commnd = NET_ROUTE_ROOM;
  514.       *send2 = NON_LOC_NET;
  515.       *send3 = LOC_NET;
  516.       *name = "Backbone - Passive";
  517.       break;
  518.       default: crashout("shared rooms: #2");
  519.  
  520.       }
  521.     break;
  522.     default: crashout("shared rooms: #1");
  523.  
  524.     }
  525.   }
  526. /*
  527. * SendRoom()
  528. *
  529. * Sends a room to the receiving system during netting.  It returns ERROR if
  530. * carrier etc is lost.
  531. */
  532. static int SendRoom(SharedRoom *room, int system, int index, int roomslot,
  533. void *d)
  534.   {
  535.   char cmd;
  536.   char doit, *s1, *s2, *s3, *name;
  537.   if (!gotCarrier())
  538.     {
  539.     modStat = haveCarrier = FALSE;
  540.     return ERROR;
  541.  
  542.     }
  543.   Addressing(system, index, &cmd, &s1, &s2, &s3, &name, &doit);
  544.   if (doit && SR_Sent[index] != 1 &&
  545.   (roomTab[roomslot].rtlastNet >  netBuf.netRooms[index].lastMess ||
  546.   GetFA(room->mode) ||
  547.   (roomTab[roomslot].rtShareType == BACKBONE &&
  548.   GetMode(room->mode) != PEON &&  !netBuf.nbflags.local))
  549.   )
  550.     {
  551.     /**
  552.     if (cfg.BoolFlags.debug)
  553.     splitF(netLog, "Why: %s && %d[%d] && ( %ld > %ld || %d || ( %s BACKBONE && %s PEON && %s LOCAL))\n",
  554.     (doit ? "TRUE":"FALSE"), SR_Sent[index],index,
  555.     roomTab[roomslot].rtlastNet,netBuf.netRooms[index].lastMess,GetFA(room->mode),
  556.     (roomTab[roomslot].rtShareType == BACKBONE ? "" : "Not"),
  557.     (GetMode(room->mode) != PEON ? "Not" : ""),
  558.     (netBuf.nbflags.local ? "" : "NOT"));
  559.     **/
  560.     ITL_optimize(TRUE);
  561.     findAndSend((RecMassTransfer || chkNeedsProcessing(index)) ?
  562.     NET_ROOM : cmd, s1, s2, s3, index,
  563.     RoomSend, roomTab[roomslot].rtname, RoomReceive);
  564.  
  565.     }
  566.   return TRUE;
  567.  
  568.   }
  569. /*
  570. * findAndSend()
  571. *
  572. * This function manages sending a room (virtual or normal) to the receiver,
  573. * handling both normal and route rooms, via function pointers.
  574. */
  575. void  findAndSend(int commnd, char *send1, char *send2, char *send3, int rover,
  576. int (*MsgSender)(int r, char *d1, char *d2, char *d3),
  577. label roomName, int (*MsgReceiver)(int r, char y))
  578.   {
  579.   struct cmd_data cmds;
  580.   extern MessageBuffer tempMess;
  581.   int  tempcount;
  582.   extern char *netRoomTemplate, *WRITE_ANY;
  583.   if (!gotCarrier()) return;
  584.   zero_struct(cmds);
  585.   cmds.command = commnd;
  586.   strCpy(cmds.fields[0], roomName);
  587.   if (commnd != ERROR)
  588.   if (!sendNetCommand(&cmds, "shared rooms"))
  589.     {
  590.     if (commnd == NET_ROUTE_ROOM)
  591.       {
  592.       findAndSend(NET_ROOM, send1, send2, send3, rover, MsgSender,
  593.       roomName, MsgReceiver); /* time to recurse */
  594.       return ;
  595.  
  596.       }
  597.     else
  598.       {
  599.       sPrintf(tempMess.mbtext, "%%s reports: %s (%s) commnd=%d", RecBuf+1,
  600.       roomName,commnd);
  601.       no_good(tempMess.mbtext, FALSE);
  602.       return ;
  603.  
  604.       }
  605.  
  606.     }
  607.   if (!ITL_SendMessages())
  608.     {
  609.     no_good("Couldn't start WC for room sharing: %s",
  610.     FALSE);
  611.     return;
  612.  
  613.     }
  614.   tempcount = (*MsgSender)(rover, send1, send2, send3);
  615.   ITL_StopSendMessages();
  616.   if( logNetResults && netDebug )
  617.     {
  618.     if( EncCount > 0)
  619.       splitF(netLog, "(%d) (%ld => %ld bytes)\n", tempcount, EncCount, ByteCount);
  620.     else splitF(netLog,"\n");
  621.     };
  622.   if (commnd == NET_ROUTE_ROOM)
  623.     {
  624.     (*MsgReceiver)(rover, FALSE);
  625.  
  626.     }
  627.  
  628.   }
  629. /*
  630. * RoomReceive()
  631. *
  632. * This function receives messages for a room.
  633. */
  634. int RoomReceive(int rover, char ReplyFirst)
  635.   {
  636.   recNetMessages(rover, roomBuf.rbname, netRoomSlot(rover), FALSE);
  637.   return 0;
  638.  
  639.   }
  640. /*
  641. * RoomSend()
  642. *
  643. * This function sends messages for a room.
  644. */
  645. int RoomSend(int rover, char *send1, char *send2, char *send3)
  646.   {
  647.   extern char PrTransmit;
  648.   char work[10];
  649.   int MsgCount = 0;
  650.   if( logNetResults && netDebug )splitF(netLog, "Sending %s ", roomBuf.rbname);
  651.   zero_struct(NetStyle);
  652.   NetStyle.addr1 = send1;
  653.   NetStyle.addr2 = send2;
  654.   NetStyle.addr3 = send3;
  655.   if (GetFA(netBuf.netRooms[rover].mode))
  656.     {
  657.     sPrintf(work, CACHE_END_NAME, netRoomSlot(rover));
  658.     if (SendPrepAsNormal(work, &MsgCount))
  659.     UnSetFA(netBuf.netRooms[rover].mode);
  660.  
  661.     }
  662.   NetStyle.sendfunc = sendITLchar;
  663.   /*   PrTransmit = FALSE; */
  664.   MsgCount += showMessages(NEWoNLY, FALSE, netBuf.netRooms[rover].lastMess,
  665.   NetRoute);
  666.   if (TrError == TRAN_SUCCESS)
  667.     {
  668.     SetHighValues(rover);
  669.     SR_Sent[rover] = 1;
  670.  
  671.     }
  672.   else
  673.     {
  674.     MsgCount = 0;
  675.  
  676.     }
  677.   return MsgCount;
  678.  
  679.   }
  680. /*
  681. * SetHighValues()
  682. *
  683. * This function sets the high message sent for a normal shared room after a
  684. * successful net session has (apparently) taken place.
  685. */
  686. void SetHighValues(int rover)
  687.   {
  688.   if (NetStyle.HiSent == 0l)
  689.     {
  690.     NetStyle.HiSent =
  691.     max(netBuf.netRooms[rover].lastMess,roomTab[thisRoom].rtlastMessage);
  692.  
  693.     }
  694.   netBuf.netRooms[rover].lastMess = NetStyle.HiSent;
  695.   netTab[thisNet].netTRooms[rover].lastMess = NetStyle.HiSent;
  696.  
  697.   }
  698. /*
  699. * SendPrepAsNormal()
  700. *
  701. * This function sends files prepared for cache sending as normal message files,
  702. * instead.
  703. */
  704. char SendPrepAsNormal(char *work, int *MsgCount)
  705.   {
  706.   char tempNm[3*NAMESIZE];
  707.   NetCacheName(tempNm, thisNet, work);
  708.   if ((netMisc = safeopen(tempNm, READ_ANY)) != NULL)
  709.     {
  710.     while (getMessage(getNetChar, TRUE, TRUE, TRUE))
  711.       {
  712.       (*MsgCount)++;
  713.       prNetStyle(0, sendITLchar, FALSE, "");
  714.  
  715.       }
  716.     fclose(netMisc);
  717.  
  718.     }
  719.   if (gotCarrier())
  720.     {
  721.     unlink(tempNm);
  722.     return TRUE;
  723.  
  724.     }
  725.   return FALSE;
  726.  
  727.   }
  728. /*
  729. * NetRoute()
  730. *
  731. * This is a worker function, returns TRUE if message sent.
  732. */
  733. char NetRoute(int status)
  734.   {
  735.   long temp;
  736.   if ((strncmp(msgBuf.mbaddr, NetStyle.addr1, strLen(NetStyle.addr1))
  737.   == SAMESTRING  ||
  738.   strncmp(msgBuf.mbaddr, NetStyle.addr2, strLen(NetStyle.addr2))
  739.   == SAMESTRING  ||
  740.   strncmp(msgBuf.mbaddr, NetStyle.addr3, strLen(NetStyle.addr3))
  741.   == SAMESTRING) &&
  742.   RoutePath(LOC_NET, msgBuf.mbaddr)     != thisNet       &&
  743.   RoutePath(NON_LOC_NET, msgBuf.mbaddr) != thisNet)
  744.     {
  745.     prNetStyle(FALSE, NetStyle.sendfunc, TRUE, "");
  746.     temp = atol(msgBuf.mbId);
  747.     NetStyle.HiSent = max(NetStyle.HiSent,temp);
  748.     return TRUE;
  749.  
  750.     }
  751.   return FALSE;
  752.  
  753.   }
  754. /*
  755. * RoutePath()
  756. *
  757. * This function returns the number of the node that routed this msg to here.
  758. * If the msg was not routed in from a BackBone, then return ERROR, which will
  759. * never match another node's #.
  760. *
  761. * 88Oct13: Now simply check for msg origin, assume if one exists that it
  762. * should be checked.  Don't remember why it is restricted to only
  763. * BACKBONE-routed msgs.  Doesn't seem necessary.
  764. */
  765. int RoutePath(char *rp, char *str)
  766.   {
  767.   if (strncmp(rp, str, strLen(rp)) == SAMESTRING)
  768.     {
  769.     if (strLen(str) != strLen(rp)) /* prevent return of 0 */
  770.     return atoi(str + 2);
  771.  
  772.     }
  773.   return ERROR;
  774.  
  775.   }
  776. /*
  777. * doSendFiles()
  778. *
  779. * This function will send files to a victim.
  780. */
  781. void doSendFiles()
  782.   {
  783.   extern char       *READ_ANY;
  784.   struct   fl_send  theFiles;
  785.   SYS_FILE          sdFile;
  786.   char              temp[8];
  787.   FILE              *fd;
  788.   ITL_optimize(FALSE);    /* try for better protocol */
  789.   sPrintf(temp, "%d.sfl", thisNet);
  790.   makeSysName(sdFile, temp, &cfg.netArea);
  791.   if ((fd = safeopen(sdFile, READ_ANY)) == NULL)
  792.     {
  793.     sPrintf(msgBuf.mbtext, "Couldn't open send file %s for %s!",
  794.     sdFile,netBuf.netName);
  795.     netResult(msgBuf.mbtext);
  796.     netBuf.nbflags.send_files = FALSE;
  797.  
  798.     }
  799.   else
  800.     {
  801.     while (getSLNet(theFiles, fd) && haveCarrier)
  802.       {
  803.       sysSendFiles(&theFiles);
  804.  
  805.       }
  806.     fclose(fd);
  807.     if (haveCarrier)
  808.       {
  809.       /* if no carrier, was an error during transmit */
  810.       unlink(sdFile);
  811.       netBuf.nbflags.send_files = FALSE;
  812.  
  813.       }
  814.  
  815.     }
  816.  
  817.   }
  818. /*
  819. * netSendFile()
  820. *
  821. * This function will send a file to another system via net.
  822. */
  823. void netSendFile(DirEntry *fn)
  824.   {
  825.   extern char     *READ_ANY;
  826.   struct cmd_data cmds;
  827.   char            mess[140];
  828.   if (!gotCarrier()) return ;
  829.   if( logNetResults && netDebug )splitF(netLog, "Send File: %s\n", fn->unambig);
  830.   zero_struct(cmds);
  831.   cmds.command = SEND_FILE;
  832.   strCpy(cmds.fields[0], fn->unambig);
  833.   sPrintf(cmds.fields[1], "%ld", (fn->FileSize + SECTSIZE - 1) / SECTSIZE);
  834.   sPrintf(cmds.fields[2], "%ld", fn->FileSize);
  835.   if (!sendNetCommand(&cmds, "send file"))
  836.     {
  837.     if (haveCarrier)
  838.       {
  839.       strCpy(mess, "%s reports: ");
  840.       strCat(mess, RecBuf + 1);
  841.       no_good(mess, FALSE);
  842.  
  843.       }
  844.  
  845.     }
  846.   else
  847.     {
  848.     SendHostFile(fn->unambig);
  849.     if (haveCarrier)
  850.       {
  851.       sPrintf(msgBuf.mbtext, "%s sent to %s.", fn->unambig,
  852.       netBuf.netName);
  853.       netResult(msgBuf.mbtext);
  854.  
  855.       }
  856.  
  857.     }
  858.  
  859.   }
  860. extern FILE *upfd;
  861. /*
  862. * askFiles()
  863. *
  864. * This function will ask for file(s) from caller.
  865. */
  866. static void fl_req_free(struct fl_req *d);
  867. void askFiles()
  868.   {
  869.   label    data2;
  870.   SYS_FILE dataFl;
  871.   char     mess[130];
  872.   char     ambiguous;
  873.   int      result = ITL_SUCCESS;
  874.   FILE     *temp;
  875.   struct   cmd_data cmds;
  876.   struct   fl_req file_data, *list;
  877.   SListBase Failed =
  878.     {
  879.     NULL, NULL, NULL, fl_req_free, NULL
  880.  
  881.     };
  882.   extern char *READ_ANY, *WRITE_ANY;
  883.   if (!gotCarrier())
  884.     {
  885.     modStat = haveCarrier = FALSE;
  886.     return ;
  887.  
  888.     }
  889.   sPrintf(data2, "%d.rfl", thisNet);
  890.   makeSysName(dataFl, data2, &cfg.netArea);
  891.   temp = safeopen(dataFl, READ_ANY);
  892.   if (temp == NULL)
  893.     {
  894.     no_good("Couldn't open room request file for %s", FALSE);
  895.     netBuf.nbflags.room_files = FALSE;
  896.  
  897.     }
  898.   else
  899.     {
  900.     ITL_optimize(FALSE);    /* try for better protocol */
  901.     while ( result == ITL_SUCCESS &&
  902.     fread(&file_data, sizeof (file_data), 1, temp) == 1 &&
  903.     gotCarrier() && result == ITL_SUCCESS)
  904.       {
  905.       if (netSetNewArea(&file_data.flArea))
  906.         {
  907.         zero_struct(cmds);
  908.         ambiguous = !(strchr(file_data.roomfile, '*') == NULL &&
  909.         strchr(file_data.roomfile, '?') == NULL);
  910.         cmds.command = (!ambiguous) ? R_FILE_REQ : A_FILE_REQ;
  911.         strCpy(cmds.fields[0], file_data.room);
  912.         strCpy(cmds.fields[1], file_data.roomfile);
  913.         if( logNetResults )splitF(netLog, "Requesting %s in %s\n", file_data.roomfile,
  914.         file_data.room);
  915.         if (!sendNetCommand(&cmds,
  916.         (!ambiguous) ? "single file request" :
  917.         "multiple file request"))
  918.           {
  919.           sPrintf(mess, "%%s reports %s for file %s in %s.", RecBuf+1,
  920.           file_data.roomfile, file_data.room);
  921.           no_good(mess, FALSE);
  922.  
  923.           }
  924.         else
  925.           {
  926.           if (ambiguous)
  927.           result = multiReceive(&file_data);
  928.           else
  929.             {
  930.             if ((result = ITL_Receive(file_data.filename, FALSE,
  931.             TRUE, putFLChar, fclose)) == ITL_SUCCESS)
  932.               {
  933.               sPrintf(msgBuf.mbtext,
  934.               "File '%s' received from %s (stored in directory %s).",
  935.               file_data.filename, netBuf.netName,
  936.               prtNetArea(&file_data.flArea));
  937.               netResult(msgBuf.mbtext);
  938.  
  939.               }
  940.  
  941.             }
  942.  
  943.           }
  944.  
  945.         }
  946.       homeSpace();
  947.  
  948.       }
  949.     if (gotCarrier())
  950.       {
  951.       fclose(temp);
  952.       unlink(dataFl);
  953.       netBuf.nbflags.room_files = FALSE;
  954.  
  955.       }
  956.     else
  957.       {
  958.       haveCarrier = modStat = FALSE;
  959.       /* Now find out what we didn't get and set up a new request queue */
  960.       do
  961.         {
  962.         /* use do loop to get the one it failed in */
  963.         list = GetDynamic(sizeof *list);
  964.         copy_struct(file_data, (*list));
  965.         AddData(&Failed, list, NULL, FALSE);
  966.  
  967.         }
  968.       while (fread(&file_data, sizeof (file_data), 1, temp) == 1);
  969.       fclose(temp);
  970.       unlink(dataFl);
  971.       upfd = fopen(dataFl, WRITE_ANY);
  972.       KillList(&Failed);
  973.       fclose(upfd);
  974.  
  975.       }
  976.  
  977.     }
  978.  
  979.   }
  980. /*
  981. * fl_req_free()
  982. *
  983. * This will write and free a file request.
  984. */
  985. static void fl_req_free(struct fl_req *d)
  986.   {
  987.   if (upfd != NULL) fwrite(d, sizeof *d, 1, upfd);
  988.   free(d);
  989.  
  990.   }
  991. /*
  992. * multiReceive()
  993. *
  994. * This function will receive multiple files.
  995. */
  996. char multiReceive(struct fl_req *file_data)
  997.   {
  998.   char        first = 1;
  999.   extern char *WRITE_ANY;
  1000.   sPrintf(msgBuf.mbtext,
  1001.   "Following files received from %s in response to request for %s from %s: ",
  1002.   netBuf.netName, file_data->roomfile, file_data->room);
  1003.   do
  1004.     {
  1005.     if (ITL_Receive(NULL, FALSE, TRUE, putFLChar, fclose) != ITL_SUCCESS ||
  1006.     !gotCarrier()) return ITL_BAD_TRANS;
  1007.     if (RecBuf[0] == 0)
  1008.       {
  1009.       /* Last file name       */
  1010.       sPrintf(lbyte(msgBuf.mbtext), " (stored in directory %s).",
  1011.       prtNetArea(&file_data->flArea));
  1012.       netResult(msgBuf.mbtext);
  1013.       return ITL_SUCCESS;
  1014.  
  1015.       }
  1016.     if (!first)
  1017.     strCat(msgBuf.mbtext, ", ");
  1018.     else
  1019.     first = FALSE;
  1020.     strCat(msgBuf.mbtext, RecBuf);
  1021.     if (ITL_Receive(RecBuf, FALSE, TRUE, putFLChar, fclose) != ITL_SUCCESS
  1022.     || !gotCarrier()) return ITL_BAD_TRANS;
  1023.  
  1024.     }
  1025.   while (1);
  1026.   return ITL_SUCCESS;
  1027.  
  1028.   }
  1029. /*
  1030. * sendNetCommand()
  1031. *
  1032. * This sends a command to the receiver.
  1033. */
  1034. char sendNetCommand(struct cmd_data *cmds, char *error)
  1035.   {
  1036.   char errMsg[100];
  1037.   int  count;
  1038.   if (cfg.BoolFlags.debug && netDebug )
  1039.     {
  1040.     splitF(netLog, "Sending Command: %d\n", cmds->command);
  1041.     splitF(netLog, " Field[0]: %s\n", cmds->fields[0]);
  1042.     splitF(netLog, " Field[1]: %s\n", cmds->fields[1]);
  1043.     splitF(netLog, " Field[2]: %s\n", cmds->fields[2]);
  1044.     splitF(netLog, " Field[3]: %s\n", cmds->fields[3]);
  1045.     };
  1046.  
  1047.   if (!ITL_Send(STARTUP))
  1048.     {
  1049.     sPrintf(errMsg, "Link failure for %s (system: %%s).", error);
  1050.     if (cmds->command != HANGUP) no_good(errMsg, TRUE);
  1051.     killConnection();
  1052.     return FALSE;
  1053.  
  1054.     }
  1055.   sendITLchar(cmds->command);
  1056.   for (count = 0; count < 4; count++)
  1057.     {
  1058.     if (cmds->fields[count][0])
  1059.       {
  1060.       mTrPrintf("%s", cmds->fields[count]);
  1061.  
  1062.       }
  1063.  
  1064.     }
  1065.   sendITLchar(0);
  1066.   ITL_Send(FINISH);
  1067.   if (cmds->command == HANGUP && !inReceive) return TRUE;
  1068.   ITL_Receive(NULL, FALSE, TRUE, putFLChar, fclose);
  1069.   if (RecBuf[0] == BAD || !gotCarrier()) return FALSE;
  1070.   return TRUE;
  1071.  
  1072.   }
  1073. /*
  1074. * sendHangUp()
  1075. *
  1076. * This sends the hangup command to receiver.
  1077. */
  1078. void sendHangUp()
  1079.   {
  1080.   struct cmd_data cmds;
  1081.   if (!gotCarrier())
  1082.     {
  1083.     modStat = haveCarrier = FALSE;
  1084.     return ;
  1085.  
  1086.     }
  1087.   zero_struct(cmds);
  1088.   cmds.command = HANGUP;
  1089.   sendNetCommand(&cmds, "HANGUP");
  1090.  
  1091.   }
  1092. /*
  1093. * no_good()
  1094. *
  1095. * This handles error messages when something really bad happens.
  1096. */
  1097. void no_good(char *str, char hup)
  1098.   {
  1099.   sPrintf(msgBuf.mbtext, str, netBuf.netName);
  1100.   if (hup)
  1101.     {
  1102.     killConnection();
  1103.  
  1104.     }
  1105.   netResult(msgBuf.mbtext);
  1106.  
  1107.   }
  1108. /*
  1109. * s_m_n()
  1110. *
  1111. * This sends mail normal (non-route mail).
  1112. */
  1113. int s_m_n()
  1114.   {
  1115.   FILE     *ptrs;
  1116.   label    fntemp;
  1117.   SYS_FILE fn;
  1118.   int      messCount = 0;
  1119.   struct   netMLstruct buf;
  1120.   extern char *READ_ANY;
  1121.   sPrintf(fntemp, "%d.ml", thisNet);
  1122.   makeSysName(fn, fntemp, &cfg.netArea);
  1123.   if ((ptrs = safeopen(fn, READ_ANY)) == NULL)
  1124.     {
  1125.     if (netBuf.nbflags.normal_mail)
  1126.       {
  1127.       sPrintf(msgBuf.mbtext, "No mail file to send to %s?",
  1128.       netBuf.netName);
  1129.       netResult(msgBuf.mbtext);
  1130.  
  1131.       }
  1132.     return 0;
  1133.  
  1134.     }
  1135.   while (getMLNet(ptrs, buf) && TrError == TRAN_SUCCESS)
  1136.     {
  1137.     if (findMessage(buf.ML_loc, buf.ML_id, TRUE))
  1138.       {
  1139.       if (netDebug)
  1140.         {
  1141.         MessageBuffer *msg = &msgBuf;
  1142.         splitF(netLog, "Message %6d Sector ID: %6d Author:%s\n",  msg->mbheadChar, msg->mbheadSector, msg->mbauth);
  1143.         splitF(netLog, "Date:%20s Time:%20s Local Id:%20s\n",  msg->mbdate,  msg-> mbtime,  msg-> mbId);
  1144.         splitF(netLog, "Human:%20s ID:%20s Room:%20s\n",  msg->mboname,  msg->mborig,  msg->mbroom);
  1145.         splitF(netLog, "Origin:%20s To:%20s\n",  msg->mbsrcId,  msg->mbto);
  1146.         splitF(netLog, "Route:%s\n", msg->mbaddr);
  1147.         splitF(netLog, "OtherNet:%s\n",msg->mbOther);
  1148.         splitF(netLog, "reply:%20s Domain:%20s\n",msg->mbreply, msg->mbdomain);
  1149.         }
  1150.       prNetStyle(0, sendITLchar, TRUE, netBuf.netName);
  1151.       messCount++;
  1152.  
  1153.       }
  1154.  
  1155.     }
  1156.   fclose(ptrs);
  1157.   if (TrError == TRAN_SUCCESS)
  1158.     {
  1159.     unlink(fn);
  1160.     return messCount;
  1161.  
  1162.     }
  1163.   killConnection();
  1164.   if( logNetResults )splitF(netLog, "\nFailed transferring mail!\n");
  1165.   return 0;
  1166.  
  1167.   }
  1168. /*
  1169. * SendHostFile()
  1170. *
  1171. * This function will send a file to the other system.
  1172. */
  1173. void SendHostFile(char *fn)
  1174.   {
  1175.   int  success;
  1176.   FILE *fd;
  1177.   extern int errno;
  1178.   extern char *READ_ANY;
  1179.   success = ((fd = safeopen(fn, READ_ANY)) != NULL);
  1180.   if (ITL_Send(STARTUP))
  1181.     {
  1182.     if (!success) mTrPrintf("System error failure, this is a bogus file.");
  1183.     else
  1184.       {
  1185.       SendThatDamnFile(fd, sendITLchar);
  1186.  
  1187.       }
  1188.     ITL_Send(FINISH);
  1189.  
  1190.     }
  1191.   if (!success)
  1192.     {
  1193.     sPrintf(msgBuf.mbtext, "System error! Couldn't open %s for %s, errno %d.",
  1194.     fn, netBuf.netName, errno);
  1195.     netResult(msgBuf.mbtext);
  1196.  
  1197.     }
  1198.  
  1199.   }
  1200.